package org.xqp.basic.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.xqp.basic.mapper.BaseMapper;
import org.xqp.basic.query.BaseQuery;
import org.xqp.basic.service.IBaseService;
import org.xqp.basic.util.PageList;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public T findById(Long id) {
        return baseMapper.loadByid(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }

    @Override
    @Transactional
    public void insert(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public PageList<T> queryList(BaseQuery query) {
        // 总条数
        Integer totals = baseMapper.queryCount(query);
        // 当前页数据
        List<T> rows = baseMapper.queryData(query);

        return new PageList<T>(totals,rows);

    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);
    }
}

